 <!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    

    <title>{$Think.config.site.title} - 权限组管理</title>

    <meta name="keywords" content="{$Think.config.site.keywords}">

    <link href="{$Think.config.site.resource_url}css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/animate.min.css" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/style.min.css?v=4.0.0" rel="stylesheet">

</head>
<style>
#listtable .fa{
	cursor:pointer;
	color:#1ab394;
	margin-right:1em;
}
</style>
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
      <div class="row">
          <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>{:lang('权限组列表')}</h5>
                
                    </div>
                    <div class="ibox-content">
                        <div class="row">
                            <div class="col-sm-3">
                            	<button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#modal-form"> {:lang('新增')}</button>
                                <button type="button" class="btn btn-sm btn-success" id="saveSelected"> {:lang('保存选中')}</button> </span>
                            </div>
                          	<div class="col-sm-8">
                                <div class="input-group">
                                <form action="" method="get" enctype="application/x-www-form-urlencoded">
                                <div class="input-group">
                                    <input type="text" placeholder="{:lang('输入')}{:lang('关键词')}" class="input-sm form-control" value="{$Think.get.keywords ?? ''}" name="keywords">
                                    <span class="input-group-btn">
                                        <button type="submit" class="btn btn-sm btn-primary"> {:lang('搜索')}</button> </span>
                                </div>
                                </form>
                                </div>
                            </div>
                            
                        </div>
                        <div class="table-responsive">
                            <table class="table table-striped" id="listtable">
                                <thead>
                                    <tr>

                                        <th width="30"><input name="checkall" id="checkall"  type="checkbox" class="i-checks" value="1"></th>
                                        <th width="80">{:lang('排序')}</th>
                                        <th width="40">ID</th>
                                        <th width="200">{:lang('组名称')}</th>
                                        <th>{:lang('说明')}</th>
                                        <th width="100">{:lang('状态')}</th>
                                        <th>{:lang('操作')}</th>
                                    </tr>
                                </thead>
                                <tbody>
                                	{volist name="list" id="vo"}
                                    <tr data-id="{$vo.id}" >
                                        <td>
                                        	
                                            <input name="ids[]"  type="checkbox" class="i-checks" value="{$vo.id}" {if ($vo.id < 4)}disabled{/if}>
                                            
                                        </td>
                                        <td><input type="text"  class="form-control" value="{$vo.listorder}"></td>
                                        <td>{$vo.id}</td>
                                        <td><input type="text"  class="form-control" value="{$vo.title}"></td>
                                        <td><input type="text" {if  class="form-control" value="{$vo.description}"></td>
                                        <td>
                                        {switch name="vo.status"}
                                            {case value="0"}<button type="button" class="btn btn-default btn-xs btn-status">{:lang('已禁用')}</button>{/case}
                                            {case value="1"}<button type="button" class="btn btn-primary btn-xs btn-status">{:lang('已启用')}</button>{/case}
                                            {case value="2"}<button type="button" class="btn btn-default btn-xs btn-status">{:lang('已禁用')}</button>{/case}
                                        {/switch}
                                        </td>
                                        <td><i class="fa fa-wrench"> {:lang('绑定权限')}</i> <i class="fa fa-trash-o"> {:lang('删除')}</i>
                                        </td>
                                    </tr>
                                   {/volist}
                                </tbody>
                            </table>
                        </div>

                    </div>
                </div>
            </div>

        </div>
    </div>
    <div id="modal-form" class="modal fade" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                  <div class="row">
                    <div class="col-sm-12">
                            <h3 class="m-t-none m-b">{:lang('新增权限组')}</h3>

                            <form id="editForm" role="form" class="form-horizontal">
                            <input type="hidden" name="status" value="1"/>
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">{:lang('权限组名称')}：</label>
                                    <div class="col-sm-8">
                                    	<input type="text" name="title"  class="form-control" value="">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">{:lang('排序编号')}：</label>
                                    <div class="col-sm-8">
                                    	<input type="text" name="listorder"  class="form-control" value="0">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-12 control-label" style="text-align:left">{:lang('说明')}：</label>
                                    <div class="col-sm-12">
                                    	<input type="text" name="description"  class="form-control">
                                    </div>
                                </div>
                                
                               
                                <div style="text-align:center;">
                                    <button class="btn btn-sm btn-primary m-t-n-xs" type="button" ><strong>{:lang('保存')}</strong>
                                    </button>
                                    
                                    <button class="btn btn-sm btn-white  m-t-n-xs" type="button" data-dismiss="modal"><strong>{:lang('取消')}</strong>
                                    </button>
                                </div>
                            </form>
                      </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="{$Think.config.site.resource_url}js/jquery.min.js?v=2.1.4"></script>
    <script src="{$Think.config.site.resource_url}js/bootstrap.min.js?v=3.3.5"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/peity/jquery.peity.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/iCheck/icheck.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/layer/layer.js"></script>
</body>

</html>
<script>

$(document).ready(function() {
	$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",});

	//全选
	$("#checkall").bind("ifChecked",function(){
		$("input[name='ids[]']").iCheck("check");
	});
	//取消全选
	$("#checkall").bind("ifUnchecked",function(){
		$("input[name='ids[]']").iCheck("uncheck");
	});
	$("#saveSelected").bind("click",function(){
		var list = [];
		$("input[name='ids[]']:checked").each(function(index, element) {
            $tr = $(this).closest("tr");
			var data = {
				id : $tr.data("id"),
				listorder : $tr.find("td:eq(1) input").val(),
				title : $tr.find("td:eq(3) input").val(),
				description : $tr.find("td:eq(4) input").val(),
			}
			list.push(data);
			
        });
		if(list.length < 1) {
			layer.msg("{:lang('请至少选中一项')}", {icon: 5});
			return false;
		}
		
		if (
			post('/ausadmin/Group/edit',window.JSON.stringify(list))
		){
			layer.msg("{:lang('保存成功！')}", {icon: 1});
		} else {
			//layer.msg('保存失败', {icon: 5});
		}
		
	});
    
	//保存按钮注册
	$("#modal-form .btn-primary").bind("click",function(){
		var formData = $("#modal-form form").serialize();
		$('#modal-form').modal('hide');
		var result = false;
		if( result = post('/ausadmin/Group/add',formData)){
			layer.msg("{:lang('新增成功！')}", {icon: 1});
			var $tr = $('<tr data-id="'+ result.data.id +'" >\
                                        <td>\
                                        	 <input name="ids[]"  type="checkbox" class="i-checks" value="'+ result.data.id +'">\
                                        </td>\
                                        <td><input type="text"  class="form-control" value="'+ result.data.listorder +'"></td>\
                                        <td>'+ result.data.id +'</td>\
                                        <td><input type="text"  class="form-control" value="'+ result.data.title +'"></td>\
                                        <td><input type="text"  class="form-control" value="'+ result.data.description +'"></td>\
                                        <td>\
                                        <button type="button" class="btn btn-primary btn-xs btn-status">{:lang(\'已启用\')}</button\
                                        </td>\
                                        <td><i class="fa fa-wrench"> {:lang(\'绑定权限\')}</i> <i class="fa fa-trash-o"> {:lang(\'删除\')}</i>\
                                        </td>\
                                    </tr>');
			$("#listtable tbody").append($tr);
			$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",});
		} else {
			layer.msg("{:lang('新增失败')}", {icon: 5});
		}

	})
	//取消按钮注册
	$("#modal-form .btn-white").bind("click",function(){
		$('#modal-form').modal('hide');
	})
	//启用/禁用按钮注册
	$("#listtable").on("click",".btn-status",function(){
		var $tr = $(this).closest("tr");
		var id = $tr.data("id");
		var status = $(this).hasClass("btn-primary") ? 0 : 1;
		if(
			post('/ausadmin/Group/edit',{
				'id' : id,
				'status' : status,
				
			})
		) {
			if( status ) {
				$(this).removeClass("btn-default").addClass("btn-primary").html("{:lang('已启用')}");

			} else {
				$(this).removeClass("btn-primary").addClass("btn-default").html("{:lang('已禁用')}");

			}
			
		}
		
	})
	
	
	
	//删除按钮注册
	$("#listtable").on("click",".fa-trash-o",function(){
		var $tr = $(this).closest("tr");
		var id = $tr.data("id");
		layer.confirm("{:lang('删除后不可恢复！确认删除？')}", {
			btn: ["{:lang('确定删除')}","{:lang('不删除')}"], //按钮
			shade: false //不显示遮罩
		}, function(){
			//确定删除
			var result = false;
			if( result = post('/ausadmin/Group/delete?id='+id) ) {
				$tr.remove();
				layer.msg(result.msg, {icon: 1});
			} 
		},
		function(){
			
		});

		
	});
	//绑定权限按钮
	$("#listtable").on("click",".fa-wrench",function(){
		var $tr = $(this).closest("tr");
		var id = $tr.data("id");
		layer.open({
		  type: 2,
		  title: "{:lang('绑定权限')}",
		  shadeClose: true,
		  shade: 0.8,
		  area: ['700px', '90%'],
		  content: '<?php echo \think\Request::instance()->root(); ?>/ausadmin/Group/rule?id='+id, //iframe的url
		  btn: ["{:lang('确定')}", "{:lang('取消')}"] //可以无限个按钮
		  ,btn1: function(index, layero){
			//按钮【按钮三】的回调
			var list = [];
			var d = $('iframe',layero)[0].contentDocument;
			$("input[name='rule[]']:checked",d).each(function(index, element) {
				list.push($(this).val());	
			});
			var rules = '';
			if(list.length > 0) {
				rules = list.join(',');
			}
			if (
				post('/ausadmin/Group/rule?id='+id, {rules : rules})
			){
				layer.msg('保存成功！', {icon: 1});
				layer.close(index);
			} 

			
		  }
	  	});
	})
	
	//提交保存
	function post(url,formData){
		var res = false;
		$.ajax({
			type : 'POST',
			url : '<?php echo \think\Request::instance()->root(); ?>'+url,
			dataType : 'json',
			data : formData,
			async:false,
			success : function(result) {
				//layer.close(layer1);
				if(0 === result.code) {
					res = result;
				}  else  {
					//修改失败
					res = false;
					layer.msg(result.msg, {icon: 5});
				}
			},
			error : function(){
				res = false;
				layer.msg("{:lang('服务器错误！请稍后重试')}", {icon: 2});
			}
		})
		return res;
	}
});	
</script>